﻿<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" Title="Validator" %>

<%@ Register TagPrefix="sudoku" TagName="grid" Src="~/Controls/Sudoku/SudokuInput.ascx" %>
<%@ Import Namespace="SudokuState" %>
<%@ Import Namespace="SolverLibrary.Data_Entities" %>
<script runat="server">
    protected override void OnLoad(EventArgs e)
    {
        if (Page.IsPostBack)
        {
            int[][] puzzle = ((SudokuState)Session["validator"]).ToArray();
            SudokuBoard board = new SudokuBoard();
            for (int row = 0; row < puzzle.Length; row++)
            {
                board.insertRow(puzzle[row], row);
            }
            System.Diagnostics.Debug.WriteLine(String.Format("Pre-solved board:\n{0}", board));
            SudokuPuzzle sudokuPuzzle = new SudokuPuzzle(board);
            Board solvedBoard = sudokuPuzzle.solve();

            System.Diagnostics.Debug.WriteLine(String.Format("Updating session to solved board:\n{0}", solvedBoard));
            Session["validator"] = SudokuState.FromMatrix(solvedBoard.getTwoDimensional());
        }
        base.OnLoad(e);
    }
</script>

<asp:Content ID="Content2" ContentPlaceHolderID="Body" runat="Server">
    <sudoku:grid ID="validatorGrid" runat="server"></sudoku:grid>
    <asp:Button runat="server" Text="Validate!" ID="validatePuzzle" />
    <script>
        window.onload = document.getElementById("validatorNavigator").setAttribute("class", "active");
    </script>
</asp:Content>
